<?php
/**
 * Created by PhpStorm.
 * User: user1
 * Date: 2018/9/3
 * Time: 15:56
 */

namespace app\admin\controller;


use app\common\service\JurisdictionService;
use app\common\validate\AuthValidate;
use app\common\validate\IDMustBePostiveInt;
use think\Exception;
use think\facade\Request;
use app\common\model\Auth as AuthModel;

class Auth extends Common
{
    /**
     * 权限列表
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index() {
        if (Request::isAjax()) {
            $auths = JurisdictionService::getAuths();
            return return_data('权限列表', 0, $auths);
        } else {
            return view();
        }
    }

    /**
     * 添加或者编辑权限
     * @return \think\response\Json|\think\response\View
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function save_auth() {
        $id = input('param.id', 0);
        if (request()->isAjax()) {

            $auth_validate = new AuthValidate();
            $auth_validate->goCheck();
            $param = $auth_validate->getDataByRule(input('param.'));

            $data = AuthModel::createOrUpdateAuth($param, $id);
            return $data;
        } else {
            $data = [];
            if ($id > 0) {
                $data = AuthModel::get($id);
            }
            $this->assign('data', $data);

            $auths = JurisdictionService::getAuths();
            $this->assign('auths', $auths);
            return view();
        }
    }

    /**
     * 删除权限
     * @return \think\response\Json
     * @throws Exception
     */
    public function delete_auth() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        try {
            AuthModel::destroy($id);
            return return_data('删除成功');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }
    }

    /**
     * 修改权限状态
     * @return \think\response\Json
     * @throws Exception
     */
    public function save_status() {
        (new IDMustBePostiveInt())->goCheck();
        try {
            $id = input('param.id');
            $auth = AuthModel::get($id);
            if (!$auth) {
                throw new Exception('权限不存在');
            }
            $status = input('param.status', 0);
            if (!in_array($status, [1,2])) {
                throw new Exception('参数错误');
            }
            $auth->save(['status' => $status]);
            return return_data('修改成功');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }
    }

    /**
     * 修改权限排序
     * @return \think\response\Json
     * @throws Exception
     */
    public function save_order() {
        (new IDMustBePostiveInt())->goCheck();
        try {
            $id = input('param.id');
            $role = AuthModel::get($id);
            if (!$role) {
                throw new Exception('角色不存在');
            }
            $order = input('param.order', 0);
            if ($order <= 0) {
                throw new Exception('参数错误');
            }
            $role->save(['order' => $order]);
            return return_data('修改成功');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }
    }
}